//
//  SessionCell.swift
//  test-ios
//
//  Created by wyz5204 on 2023/7/10.
//

import Foundation
import Reusable
import SnapKit
import Kingfisher

class SessionCell : AbstractTableViewCell<ChatSession> , Reusable {
    
    private lazy var headerImage = {
        return UIImageView().then { view in
            view.layer.cornerRadius = 8 // 设置圆角
            view.layer.masksToBounds = true
            view.contentMode = .scaleAspectFill
        }
    }()
    
    private lazy var titleView = {
        return UILabel()
    }()
    
    private lazy var lastMessageView = {
        return UILabel()
    }()
    
    private lazy var unreadRedView = {
        return RedUnreadView()
    }()
    
    private lazy var sessionType = {
        return UILabel()
    }()
    
    override func initView() {
        headerImage.addTo(self.contentView) { view in
            view.snp.makeConstraints { make in
                make.top.equalToSuperview().offset(8);
                make.left.equalToSuperview().offset(15);
                make.width.height.equalTo(44)
            }
        }
        
        titleView.addTo(self.contentView) { view in
            view.snp.makeConstraints { make in
                make.bottom.equalTo(self.headerImage.snp.centerY).offset(-1);
                make.left.equalTo(self.headerImage.snp.right).offset(12);
            }
        }
        unreadRedView.addTo(self.contentView) { view in
            view.snp.makeConstraints { make in
                make.top.equalToSuperview().offset(20)
                make.right.equalToSuperview().offset(-20)
                make.height.equalTo(16)
                make.width.equalTo(16)
            }
        }
    }
    
    override func initBinder(_ view: UITableView, _ session: ChatSession, _ index: IndexPath) {
        
        // 设置session name
        titleView.setTitleAndColor(session.name)
        
        // 设置session name
        unreadRedView.num = session.unreadNum
        
        // 设置头像
        let imagePath = session.image
        
        if(imagePath?.isEmpty ?? true){
            // path == nil / ""
            // 判断是否是 组织/项目/审批/系统消息
            headerImage.image = UIImage(named: "im_group_icon")
            
        }else{
            let url = URL(string: imagePath!)
            Logger.e(url , headerImage.bounds.size)
            let processor = DownsamplingImageProcessor(size: CGSize(width: 44, height: 44))
            |> RoundCornerImageProcessor(cornerRadius: 8)
            headerImage.kf.indicatorType = .activity
            headerImage.kf.setImage(
                with: url,
                placeholder: UIImage(named: "im_applet_icon"),
                options: [
                    .processor(processor),
                    .scaleFactor(UIScreen.main.scale),
                    .transition(.fade(1)),
                    .cacheOriginalImage
                ])
            { result in
//                switch result {
//                case .success(let value):
//                    Logger.e("Task done for: \(value.source.url?.absoluteString ?? "")")
//                case .failure(let error):
//                    Logger.e(session.name , "Job failed: \(error.localizedDescription)")
//                }
            }
        }
    }
}

